import Cookies from 'js-cookie'
import config from '@/config'
import { getRegisterInfo, getOrphanByCard } from '@/api/stree'

export const TOKEN_KEY = 'token'

const { cookieExpires } = config

export const setToken = (token) => {
  Cookies.set(TOKEN_KEY, token, { expires: cookieExpires || 1 })
  if (token === '' || token === undefined) {
    console.log('晴空cookies')
    clearCookies()
  }
}

export const getToken = () => {
  const token = Cookies.get(TOKEN_KEY)
  if (token) return token
  else return false
}
export const clearCookies = () => {
  Cookies.remove('juyou.session.id')
}
export const getRouteTitleHandled = (route) => {
  let router = { ...route }
  let meta = { ...route.meta }
  let title = ''
  if (meta.title) {
    if (typeof meta.title === 'function') {
      meta.__titleIsFunction__ = true
      title = meta.title(router)
    } else title = meta.title
  }
  meta.title = title
  router.meta = meta
  return router
}
export const setTitle = (routeItem, vm) => {
  const handledRoute = getRouteTitleHandled(routeItem)
  const pageTitle = showTitle(handledRoute, vm)
  const resTitle = pageTitle ? `${title} - ${pageTitle}` : title
  window.document.title = resTitle
  if (routeItem.name === 'yiQingStatisticsMobile') {
    window.document.title = pageTitle
  }
}
/**
 * @param {Array} routeMetched 当前路由metched
 * @returns {Array}
 */
export const getBreadCrumbList = (route, homeRoute) => {
  console.log(route, homeRoute, '执行函数')
  let homeItem = { ...homeRoute, icon: homeRoute.meta.icon }
  let routeMetched = route.matched
  if (routeMetched.some(item => item.name === homeRoute.name)) return [homeItem]
  let res = routeMetched.filter(item => {
    return item.meta === undefined || !item.meta.hideInBread
  }).map(item => {
    let meta = { ...item.meta }
    if (meta.title && typeof meta.title === 'function') {
      meta.__titleIsFunction__ = true
      meta.title = meta.title(route)
    }
    let obj = {
      icon: (item.meta && item.meta.icon) || '',
      name: item.name,
      meta: meta
    }
    return obj
  })
  res = res.filter(item => {
    return !item.meta.hideInMenu
  })
  return [{ ...homeItem, to: homeRoute.path }, ...res]
}
/**
 * @param {Array} routers 路由列表数组
 * @description 用于找到路由列表中name为home的对象
 */
export const getHomeRoute = (routers, homeName = 'home') => {
  let i = -1
  let len = routers.length
  let homeRoute = {}
  while (++i < len) {
    let item = routers[i]
    if (item.children && item.children.length) {
      let res = getHomeRoute(item.children, homeName)
      if (res.name) return res
    } else {
      if (item.name === homeName) homeRoute = item
    }
  }
  return homeRoute
}

export const showTitle = (item, vm) => {
  let { title } = item.meta
  if (!title) return
  title = (item.meta && item.meta.title) || item.name
  return title
}

export const phpMenusfilter = (data) => {
  return data
}
export const javaMenusfilter = (data) => {
  return data
}
export function findRegisterInfo (cardno, name) {
  let param = { 'IDcard': cardno, 'Name': name }
  let register = ''
  getRegisterInfo(param).then(ret => {
    if (ret.data.errcode === 0) {
      if (ret.data.data.result === '-1') {
        register = '非沈阳户籍地'
      } else if (ret.data.data.result === '0') {
        register = '沈阳有效户籍'
      } else if (ret.data.data.result === '1') {
        register = '户籍已删除'
      } else if (ret.data.data.result === '2') {
        register = '户籍被加锁'
      } else if (ret.data.data.result === '3') {
        register = '重错号'
      } else if (ret.data.data.result === '4') {
        register = '跨市重错号'
      } else if (ret.data.data.result === '6') {
        register = '持证'
      } else if (ret.data.data.result === '7') {
        register = '在逃犯'
      } else if (ret.data.data.result === '8') {
        register = '户籍已迁出沈阳'
      } else if (ret.data.data.result === '9') {
        register = '死亡'
      }
      return
    }
    this.$Modal.error({
      title: '错误',
      content: ret.data.errmsg
    })
  })
  return register
}
export function findOrphanByCard (cardno) {
  let param = { 'cardno': cardno }
  getOrphanByCard(param).then(res => {
    if (res.data.errcode === 0) return true
    else return false
  })
}
